package com.me.waterphase.listener;

import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.me.waterphase.utils.PhysicManager;
import com.me.waterphase.listener.contact.*;

/*
 * Dispatch event to custom listener.
 */
public class ListenerDispatcher implements ContactListener {

	/*
	 * At the beginning of the contact.
	 * @see com.badlogic.gdx.physics.box2d.ContactListener#beginContact(com.badlogic.gdx.physics.box2d.Contact)
	 */
	@Override
	public void beginContact(Contact contact) {

		if(contact.getFixtureA().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureA().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.beginContact(contact, true);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.beginContact(contact, true);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.beginContact(contact, true);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.beginContact(contact, true);
					break;
			}
		}
		if(contact.getFixtureB().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureB().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.beginContact(contact, false);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.beginContact(contact, false);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.beginContact(contact, false);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.beginContact(contact, false);
					break;
			}
		}
	}

	/*
	 * At the end of the contact.
	 * @see com.badlogic.gdx.physics.box2d.ContactListener#endContact(com.badlogic.gdx.physics.box2d.Contact)
	 */
	@Override
	public void endContact(Contact contact) {
		if(contact.getFixtureA().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureA().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.endContact(contact, true);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.endContact(contact, true);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.endContact(contact, true);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.endContact(contact, true);
					break;
			}
		}
		if(contact.getFixtureB().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureB().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.endContact(contact, false);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.endContact(contact, false);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.endContact(contact, false);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.endContact(contact, false);
					break;
			}
		}
		
	}

	/*
	 * Pre-solve treament.
	 * @see com.badlogic.gdx.physics.box2d.ContactListener#preSolve(com.badlogic.gdx.physics.box2d.Contact, com.badlogic.gdx.physics.box2d.Manifold)
	 */
	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		if(contact.getFixtureA().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureA().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.preSolve(contact, oldManifold, true);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.preSolve(contact, oldManifold, true);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.preSolve(contact, oldManifold, true);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.preSolve(contact, oldManifold, true);
					break;
			}
		}
		if(contact.getFixtureB().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureB().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.preSolve(contact, oldManifold, true);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.preSolve(contact, oldManifold, false);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.preSolve(contact, oldManifold, false);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.preSolve(contact, oldManifold, false);
					break;
			}
		}
		
	}

	/*
	 * Post solve treatment.
	 * @see com.badlogic.gdx.physics.box2d.ContactListener#postSolve(com.badlogic.gdx.physics.box2d.Contact, com.badlogic.gdx.physics.box2d.ContactImpulse)
	 */
	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		if(contact.getFixtureA().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureA().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.postSolve(contact, impulse, true);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.postSolve(contact, impulse, true);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.postSolve(contact, impulse, true);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.postSolve(contact, impulse, true);
					break;
			}
		}
		if(contact.getFixtureB().getUserData() != null)
		{
			PhysicManager.CONTACT_TYPE type = ((ICustomContact)contact.getFixtureB().getUserData()).getContactType();
			
			switch(type)
			{
				case USERDATA_ARTIFACT:
					ArtifactListener.postSolve(contact, impulse, false);
					break;
				case USERDATA_PLAYER:
					break;
				case USERDATA_GROUND_SENSOR:
					GroundListener.postSolve(contact, impulse, false);
					break;
				case USERDATA_EDGE_SENSOR:
					EdgeListener.postSolve(contact, impulse, false);
					break;
				case USERDATA_ENERGY_SENSOR:
					EnergyListener.postSolve(contact, impulse, false);
					break;
			}
		}
		
	}
	

}
